<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page language="java" import="java.io.BufferedReader" %>
<%@ page language="java" import="java.io.FileReader" %>
<%@ page language="java" %>
<%@ page import="java.text.*"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String webSocketPath = request.getServerName() + ":" + request.getServerPort() + path;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title>WebGL模型页面</title>

    <link rel="stylesheet" type="text/css" href="mintor/compensatormanage/js/webuploader.css"/>

    <script type="text/javascript" src="jquery/jquery-easyui-1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/dat.gui.min.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/three.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/Detector.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/OrbitControls.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/VRMLLoader.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/stats.min.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/Tween.js"></script>
    <script type="text/javascript" src="mintor/model/threeJs/RequestAnimationFrame.js"></script>


    <script type="text/javascript" src="mintor/compensatormanage/js/webuploader.js"></script>

    <style>
        body {
            margin: 0;
            overflow: hidden;
        }

        .lbl {
            color: #fff;
            font-size: 16px;
            font-weight: bold;
            position: absolute;
            z-index: 100;
            text-shadow: #000 1px 1px 1px;
            background-color: rgba(0, 0, 0, 0.85);
            padding: 1em
        }
        .lbl1 {
            color: #fff;
            font-size: 16px;
            font-weight: bold;
            position: absolute;
            z-index: 100;
            text-shadow: #000 1px 1px 1px;
            background-color: rgba(0, 0, 1, 0.85);
            padding: 1em
        }

        #lbl_tip {
            text-align: left;
        }

        .g {
            color: #aaa
        }

        .c {
            color: #fa0
        }

        img {
            z-index: -1;
            position: relative;
            height: 80px;
            width: 80px;
            /*height:15%;*/
        }

        .imgfangda {
            position: absolute;
            z-index: 100;
            background-color: rgba(0, 0, 0, 0);
            /*padding: 1em*/
        }

    </style>
</head>


<body>

<script type="text/javascript">
    $(function () {
        // 变色特效
        var backgroundElement = window.top.cfgetTopBackgroundElement();
        $(backgroundElement).css("background", "rgba(235,245,235,0.50)");

        $("div.img :input").not(".lbl1").hover(function (e) {
//        $(":input").hover(function (e) {
            var $addd = $(this).attr("src");
            document.getElementById("imgfangda").style.left = e.clientX + 20 + "px";
            document.getElementById("imgfangda").style.top = e.clientY + "px";
            $(".imgfangda").attr('src', $addd).show().css("opacity", "1");
        }, function () {
            $(".imgfangda").hide();
        });
    });
</script>

<div id="lbl_tip" class="lbl" style="display: none;">
    <p2 id="sensorNum">传感器编号: <span class="c">001-SDT00-04</span><br/></p2>
    <p2 id="sensorValue">数值: <span class="c">28.5度</span><br/></p2>
</div>


<div class="img">
    <div id="info_tip" class="lbl" style="left:0;top: 60px;
            width: 306px; height:300px;
            background-size: 100% 100%;
            background-image: url('<%=basePath%>/mintor/model/png/board.png');
            border-radius: 3%;">
        <div id="board_context">
            <div id="pos" style="position: absolute;left: 30px; top: 40px;display:block;font-size: smaller">
                <p2 id="xpos"> x轴坐标 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="ypos"> y轴坐标 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="zpos"> z轴坐标 ：<span class="c">0.00000000</span><br/></p2>
            </div>
            <div id="pwr" style="position: absolute;left: 30px; top: 40px;display:none;font-size: smaller">
                <p2 id="xpwr"> x轴功率 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="ypwr"> y轴功率 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="zpwr"> z轴功率 ：<span class="c">0.00000000</span><br/></p2>
            </div>
            <div id="fuhe" style="position: absolute;left: 30px; top: 40px;display:none;font-size: smaller">
                <p2 id="xfu"> x轴负荷 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="yfu"> y轴负荷 ：<span class="c">0.00000000</span><br/></p2>
                <p2 id="zfu"> z轴负荷 ：<span class="c">0.00000000</span><br/></p2>
            </div>
            <p2 id="maspd" style="position: absolute;left: 30px; top: 95px;font-size: smaller">主轴速度：<span class="c">0.00000000</span><br/>
            </p2>

            <input id="left_t" type="image" src='mintor/model/png/left_t.png'
                   style="position: absolute; top: 175px; left: 43px; width: 18px;">
            <input id="right_t" type="image" src='mintor/model/png/right_t.png'
                   style="position: absolute; top: 175px; left: 167px; width: 18px;">
            <input id="mode_sel" type="image" src='mintor/model/png/left_sel.png'
                   style="position: absolute; top: 215px; left: 23px; width: 35px;">
            <input id="axis_sel" type="image" src='mintor/model/png/left_sel.png'
                   style="position: absolute; top: 215px; left: 106px; width: 35px;">
            <input id="rate_sel" type="image" src='mintor/model/png/left_sel.png'
                   style="position: absolute; top: 215px; left: 185px; width: 35px;">
            <input id="pause_sel" type="image" src='mintor/model/png/pause_off.png'
                   style="position: absolute; top: 205px; left: 265px; width: 50px;">
            <input id="cycle_on" type="image" src='mintor/model/png/green_off.png'
                   style="position: absolute; top: 266px; left: 24px; width: 33px;">
            <input id="cycle_off" type="image" src='mintor/model/png/red_off.png'
                   style="position: absolute; top: 304px; left: 24px; width: 33px;">

            <input id="press_o" type="image" src='mintor/model/png/press_o.png'
                   style="position: absolute; top: 14px; left: 209px; width: 18px;">
            <input id="press_n" type="image" src='mintor/model/png/press_n.png'
                   style="position: absolute; top: 14px; left: 228px; width: 18px;">
            <input id="press_g" type="image" src='mintor/model/png/press_g.png'
                   style="position: absolute; top: 14px; left: 247px; width: 18px;">
            <input id="press_7" type="image" src='mintor/model/png/press_7.png'
                   style="position: absolute; top: 14px; left: 266px; width: 18px;">
            <input id="press_8" type="image" src='mintor/model/png/press_8.png'
                   style="position: absolute; top: 14px; left: 285px; width: 18px;">
            <input id="press_9" type="image" src='mintor/model/png/press_9.png'
                   style="position: absolute; top: 14px; left: 304px; width: 18px;">

            <input id="press_x" type="image" src='mintor/model/png/press_x.png'
                   style="position: absolute; top: 33px; left: 209px; width: 18px;">
            <input id="press_y" type="image" src='mintor/model/png/press_y.png'
                   style="position: absolute; top: 33px; left: 228px; width: 18px;">
            <input id="press_z" type="image" src='mintor/model/png/press_z.png'
                   style="position: absolute; top: 33px; left: 247px; width: 18px;">
            <input id="press_4" type="image" src='mintor/model/png/press_4.png'
                   style="position: absolute; top: 33px; left: 266px; width: 18px;">
            <input id="press_5" type="image" src='mintor/model/png/press_5.png'
                   style="position: absolute; top: 33px; left: 285px; width: 18px;">
            <input id="press_6" type="image" src='mintor/model/png/press_6.png'
                   style="position: absolute; top: 33px; left: 304px; width: 18px;">

            <input id="press_m" type="image" src='mintor/model/png/press_m.png'
                   style="position: absolute; top: 52px; left: 209px; width: 18px;">
            <input id="press_s" type="image" src='mintor/model/png/press_s.png'
                   style="position: absolute; top: 52px; left: 228px; width: 18px;">
            <input id="press_t" type="image" src='mintor/model/png/press_t.png'
                   style="position: absolute; top: 52px; left: 247px; width: 18px;">
            <input id="press_1" type="image" src='mintor/model/png/press_1.png'
                   style="position: absolute; top: 52px; left: 266px; width: 18px;">
            <input id="press_2" type="image" src='mintor/model/png/press_2.png'
                   style="position: absolute; top: 52px; left: 285px; width: 18px;">
            <input id="press_3" type="image" src='mintor/model/png/press_3.png'
                   style="position: absolute; top: 52px; left: 304px; width: 18px;">

            <input id="press_f" type="image" src='mintor/model/png/press_f.png'
                   style="position: absolute; top: 71px; left: 209px; width: 18px;">
            <input id="press_h" type="image" src='mintor/model/png/press_h.png'
                   style="position: absolute; top: 71px; left: 228px; width: 18px;">
            <input id="press_eob" type="image" src='mintor/model/png/press_eob.png'
                   style="position: absolute; top: 71px; left: 247px; width: 18px;">
            <input id="press_minus" type="image" src='mintor/model/png/press_minus.png'
                   style="position: absolute; top: 71px; left: 266px; width: 18px;">
            <input id="press_0" type="image" src='mintor/model/png/press_0.png'
                   style="position: absolute; top: 71px; left: 285px; width: 18px;">
            <input id="press_point" type="image" src='mintor/model/png/press_point.png'
                   style="position: absolute; top: 71px; left: 304px; width: 18px;">

            <input id="press_pos" type="image" src='mintor/model/png/press_pos.png'
                   style="position: absolute; top: 90px; left: 209px; width: 18px;">
            <input id="press_prog" type="image" src='mintor/model/png/press_prog.png'
                   style="position: absolute; top: 90px; left: 228px; width: 18px;">
            <input id="press_offset" type="image" src='mintor/model/png/press_offset.png'
                   style="position: absolute; top: 90px; left: 247px; width: 18px;">
            <input id="press_shift" type="image" src='mintor/model/png/press_shift.png'
                   style="position: absolute; top: 90px; left: 266px; width: 18px;">
            <input id="press_can" type="image" src='mintor/model/png/press_can.png'
                   style="position: absolute; top: 90px; left: 285px; width: 18px;">
            <input id="press_input" type="image" src='mintor/model/png/press_input.png'
                   style="position: absolute; top: 90px; left: 304px; width: 18px;">

            <input id="press_system" type="image" src='mintor/model/png/press_system.png'
                   style="position: absolute; top: 109px; left: 209px; width: 18px;">
            <input id="press_message" type="image" src='mintor/model/png/press_message.png'
                   style="position: absolute; top: 109px; left: 228px; width: 18px;">
            <input id="press_custom" type="image" src='mintor/model/png/press_custom.png'
                   style="position: absolute; top: 109px; left: 247px; width: 18px;">
            <input id="press_alter" type="image" src='mintor/model/png/press_alter.png'
                   style="position: absolute; top: 109px; left: 266px; width: 18px;">
            <input id="press_insert" type="image" src='mintor/model/png/press_insert.png'
                   style="position: absolute; top: 109px; left: 285px; width: 18px;">
            <input id="press_delete" type="image" src='mintor/model/png/press_delete.png'
                   style="position: absolute; top: 109px; left: 304px; width: 18px;">

            <input id="press_pageup" type="image" src='mintor/model/png/press_pageup.png'
                   style="position: absolute; top: 128px; left: 209px; width: 18px;">
            <input id="press_up" type="image" src='mintor/model/png/press_up.png'
                   style="position: absolute; top: 128px; left: 247px; width: 18px;">
            <input id="press_help" type="image" src='mintor/model/png/press_help.png'
                   style="position: absolute; top: 128px; left: 304px; width: 18px;">

            <input id="press_pagedown" type="image" src='mintor/model/png/press_pagedown.png'
                   style="position: absolute; top: 147px; left: 209px; width: 18px;">
            <input id="press_down" type="image" src='mintor/model/png/press_down.png'
                   style="position: absolute; top: 147px; left: 247px; width: 18px;">
            <input id="press_reset" type="image" src='mintor/model/png/press_reset.png'
                   style="position: absolute; top: 147px; left: 304px; width: 18px;">

            <input id="press_left" type="image" src='mintor/model/png/press_left.png'
                   style="position: absolute; top: 138px; left: 228px; width: 18px;">
            <input id="press_right" type="image" src='mintor/model/png/press_right.png'
                   style="position: absolute; top: 138px; left: 266px; width: 18px;">

            <input id="press_spindle_plus_off" type="image" src='mintor/model/png/press_spindle_plus_off.png'
                   style="position: absolute; top: 288px; left: 154px; width: 18px;">
            <input id="press_spindle_stop_off" type="image" src='mintor/model/png/press_spindle_stop_off.png'
                   style="position: absolute; top: 288px; left: 174px; width: 18px;">
            <input id="press_spindle_minus_off" type="image" src='mintor/model/png/press_spindle_minus_off.png'
                   style="position: absolute; top: 288px; left: 194px; width: 18px;">

            <input id="press_axis_plus_off" type="image" src='mintor/model/png/press_axis_plus_off.png'
                   style="position: absolute; top: 308px; left: 154px; width: 18px;">
            <input id="press_axis_minus_off" type="image" src='mintor/model/png/press_axis_minus_off.png'
                   style="position: absolute; top: 308px; left: 194px; width: 18px;">

            <input id="press_singlerun_off" type="image" src='mintor/model/png/press_singlerun_off.png'
                   style="position: absolute; top: 288px; left: 68px; width: 18px;">
            <input id="press_norun_off" type="image" src='mintor/model/png/press_norun_off.png'
                   style="position: absolute; top: 288px; left: 88px; width: 18px;">
            <input id="press_selectjump_off" type="image" src='mintor/model/png/press_selectjump_off.png'
                   style="position: absolute; top: 288px; left: 108px; width: 18px;">
            <input id="press_selectstop_off" type="image" src='mintor/model/png/press_selectstop_off.png'
                   style="position: absolute; top: 288px; left: 128px; width: 18px;">

            <input id="press_machinelock_off" type="image" src='mintor/model/png/press_machinelock_off.png'
                   style="position: absolute; top: 308px; left: 68px; width: 18px;">
            <input id="press_zlock_off" type="image" src='mintor/model/png/press_zlock_off.png'
                   style="position: absolute; top: 308px; left: 88px; width: 18px;">
            <input id="press_mstlock_off" type="image" src='mintor/model/png/press_mstlock_off.png'
                   style="position: absolute; top: 308px; left: 108px; width: 18px;">
            <input id="press_insert_off" type="image" src='mintor/model/png/press_insert_off.png'
                   style="position: absolute; top: 308px; left: 128px; width: 18px;">
        </div>
        <div id="ctr_hide_tip"
             style="cursor: pointer; position: absolute;bottom: 3%;right: 3%;text-align: center;font-size: small;">收起
        </div>
    </div>
    <div id="ctr_show_tip" class="lbl"
         style="cursor: pointer;display: none; position: absolute;left: 0;top: 60px;text-align: center;font-size: small;">
        展开
    </div>
    <div id="example_show" class="lbl"
         style="cursor: pointer;position: absolute;right: 0;top: 60px;text-align: center;font-size: small;">演示
    </div>

</div>


<div class="lbl1" style="font-size: small; width:180px;position: absolute;right: 0;top: 120px;height:220px;border: 1px solid gainsboro   ">
    <p style="margin-top: 5px;" align="center" >加工过程模拟</p>
    <p style="margin-top: 6px;" align="center">
        <input id="picker" type="file" style="display: none;" onchange="onFileSelected(event)">
        <button id="pickerbtn" class="btn btn-success center" style="font-size: small;margin-left: 30px ;margin-bottom:5px;border-width: 4px;font-size: 13px;
            padding: 4px 9px;line-height: 1;" >choose file
        </button>
    </p>

    <%--<p style="margin-top: 10px; font-size: small;" align="center">G代码:</p>--%>
    <div style="margin-top: 6px;text-align: center;font-size: small;">
        <textarea id="result" class="result;" style="display: none; font-size: small;"></textarea>
    </div>
    <p  align="center">
    <div sstyle="margin-top: 8px;text-align: center;font-size: small;">
        <center><button id="start" style="display: none;font-size: small;">Start</button>
            <button id="stop" style="display: none;font-size: small;">Stop</button></center>
    </div>
    </p>
</div>


<div id="control_tip" class="lbl" style="right:0; width:18%; text-align: center; display: none">
    <div id="control_display" style="text-align: center;">
        <div>模式：
            <input id="auto_btn" type="image" src='mintor/model/png/auto.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="hand_btn" type="image" src='mintor/model/png/hand.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="adj_btn" type="image" src='mintor/model/png/adjust.png'
                   style="width: 16%; vertical-align: middle;"/>
        </div>

        <div><br>轴选：
            <input id="xaxis_btn" type="image" src='mintor/model/png/xaxis.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="yaxis_btn" type="image" src='mintor/model/png/yaxis.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="zaxis_btn" type="image" src='mintor/model/png/zaxis.png'
                   style="width: 16%; vertical-align: middle;"/>
        </div>

        <div><br>倍选：
            <input id="x1_btn" type="image" src='mintor/model/png/x1.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="x10_btn" type="image" src='mintor/model/png/x10.png'
                   style="width: 16%; vertical-align: middle;"/>
            <input id="x100_btn" type="image" src='mintor/model/png/x100.png'
                   style="width: 16%; vertical-align: middle;"/>
        </div>
        <br/>
    </div>
    <%--<div id="ctr_hide_tip" style="text-align: center;font-size: small;">收起</div>--%>
</div>
</div>
<img class="imgfangda" id="imgfangda" src="" style="display: none;">

<div id="Stats-output"></div>
<div id="WebGL-output"></div>

<script type="text/javascript">
    if (!Detector.webgl) Detector.addGetWebGLMessage();
    var controls;                                           // 鼠标响应控制变量
    var container, scene, webGLRenderer, camera;
    var maxis, columnaxis, xaxismotion, yaxismotion, zaxismotion, totalMd, sensor, raycaster, mouse;
    var machineMotion;                              // 机床动作组
    var maxisGroup = undefined;                     // 主轴动作组
    var maxisBase = {x: -1, y: 1.5, z: 0};                 // 主轴基准坐标
    var maxisDeviation = {x: 0, y: 0, z: 0};               // 主轴初始化后偏移
    var axisBase = {x: 0, y: 0, z: 0};                // 三轴基准坐标
    var maxisOrigin = {x: 0, y: 0, z: 0};                  // 主轴原始坐标
    var runStatus = false, xdir = false, zdir = false, ydir = false;
    var maxiGroupRotateSpeed = 0;                    // 主轴旋转速度
    var mode_sel_v = 0, axis_sel_v = 0, rate_sel_v = 0;         // 模式选择，轴选择与倍率选择变量

    var socketPath = "<%=webSocketPath%>";
    var postionarr, xaxis, yaxis, zaxis, mspeed;
    var temdata = 0;
    var temdatas = "";
    var webSocket = new WebSocket('ws://' + socketPath + '/websocket/CFlower');

    webSocket.onerror = function (event) {			// socket错误响应事件
        onError(event);
    };
    webSocket.onopen = function (event) {			// socket开启响应事件
        onOpen(event);
    };
    webSocket.onmessage = function (event) {		// socket接收响应事件
        temdatas = event.data;
        onMessage(event);
        console.log(event.data);
    };


    init();
    render();

    $(window).resize(function () {
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);
    });

    $("#auto_btn").click(function () {
        $("#auto_btn").attr("src", 'mintor/model/png/auto_sel.png');
        $("#hand_btn").attr("src", 'mintor/model/png/hand.png');
        $("#adj_btn").attr("src", 'mintor/model/png/adjust.png');
    });
    $("#hand_btn").click(function () {
        $("#auto_btn").attr("src", 'mintor/model/png/auto.png');
        $("#hand_btn").attr("src", 'mintor/model/png/hand_sel.png');
        $("#adj_btn").attr("src", 'mintor/model/png/adjust.png');
    });
    $("#adj_btn").click(function () {
        $("#auto_btn").attr("src", 'mintor/model/png/auto.png');
        $("#hand_btn").attr("src", 'mintor/model/png/hand.png');
        $("#adj_btn").attr("src", 'mintor/model/png/adjust_sel.png');
    });
    $("#xaxis_btn").click(function () {
        $("#xaxis_btn").attr("src", 'mintor/model/png/xaxis_sel.png');
        $("#yaxis_btn").attr("src", 'mintor/model/png/yaxis.png');
        $("#zaxis_btn").attr("src", 'mintor/model/png/zaxis.png');
    });
    $("#yaxis_btn").click(function () {
        $("#xaxis_btn").attr("src", 'mintor/model/png/xaxis.png');
        $("#yaxis_btn").attr("src", 'mintor/model/png/yaxis_sel.png');
        $("#zaxis_btn").attr("src", 'mintor/model/png/zaxis.png');
    });
    $("#zaxis_btn").click(function () {
        $("#xaxis_btn").attr("src", 'mintor/model/png/xaxis.png');
        $("#yaxis_btn").attr("src", 'mintor/model/png/yaxis.png');
        $("#zaxis_btn").attr("src", 'mintor/model/png/zaxis_sel.png');
    });
    $("#x1_btn").click(function () {
        $("#x1_btn").attr("src", 'mintor/model/png/x1_sel.png');
        $("#x10_btn").attr("src", 'mintor/model/png/x10.png');
        $("#x100_btn").attr("src", 'mintor/model/png/x100.png');
    });
    $("#x10_btn").click(function () {
        $("#x1_btn").attr("src", 'mintor/model/png/x1.png');
        $("#x10_btn").attr("src", 'mintor/model/png/x10_sel.png');
        $("#x100_btn").attr("src", 'mintor/model/png/x100.png');
    });
    $("#x100_btn").click(function () {
        $("#x1_btn").attr("src", 'mintor/model/png/x1.png');
        $("#x10_btn").attr("src", 'mintor/model/png/x10.png');
        $("#x100_btn").attr("src", 'mintor/model/png/x100_sel.png');
    });
    $("#ctr_hide_tip").click(function () {
        $("#info_tip").hide();
        $("#ctr_show_tip").show();
    });
    $("#ctr_show_tip").click(function () {
        $("#ctr_show_tip").hide();
        $("#info_tip").show();
    });

    $("#mode_sel").mousedown(function (event) {
        change_sel(event.which, "#mode_sel");
    });
    $("#axis_sel").mousedown(function (event) {
        change_sel(event.which, "#axis_sel");
    });
    $("#rate_sel").mousedown(function (event) {
        change_sel(event.which, "#rate_sel");
    });
    function change_sel(eventNum, idNum) {
        if (eventNum == 1) {
            if ($(idNum).attr("src") == 'mintor/model/png/mid_sel.png') {
                $(idNum).attr("src", 'mintor/model/png/left_sel.png');
                switch (idNum) {
                    case "#mode_sel":
                        mode_sel_v = 0;
                        break;
                    case "#axis_sel":
                        axis_sel_v = 0;
                        break;
                    case "#rate_sel":
                        rate_sel_v = 0;
                        break;
                    default:
                        break;
                }
            }
            else if ($(idNum).attr("src") == 'mintor/model/png/right_sel.png') {
                $(idNum).attr("src", 'mintor/model/png/mid_sel.png');
                switch (idNum) {
                    case "#mode_sel":
                        mode_sel_v = 1;
                        break;
                    case "#axis_sel":
                        axis_sel_v = 1;
                        break;
                    case "#rate_sel":
                        rate_sel_v = 1;
                        break;
                    default:
                        break;
                }
            }
        }
        else if (eventNum == 3) {
            if ($(idNum).attr("src") == 'mintor/model/png/mid_sel.png') {
                $(idNum).attr("src", 'mintor/model/png/right_sel.png');
                switch (idNum) {
                    case "#mode_sel":
                        mode_sel_v = 2;
                        break;
                    case "#axis_sel":
                        axis_sel_v = 2;
                        break;
                    case "#rate_sel":
                        rate_sel_v = 2;
                        break;
                    default:
                        break;
                }
            }
            else if ($(idNum).attr("src") == 'mintor/model/png/left_sel.png') {
                $(idNum).attr("src", 'mintor/model/png/mid_sel.png');
                switch (idNum) {
                    case "#mode_sel":
                        mode_sel_v = 1;
                        break;
                    case "#axis_sel":
                        axis_sel_v = 1;
                        break;
                    case "#rate_sel":
                        rate_sel_v = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    $("#pause_sel").click(function () {              // 急停按钮相应事件
        // 急停状态按下
        if ($("#pause_sel").attr("src") == 'mintor/model/png/pause_on.png')
            $("#pause_sel").attr("src", 'mintor/model/png/pause_off.png');
        else {                                       // 非急停状态下按下
            $("#pause_sel").attr("src", 'mintor/model/png/pause_on.png');
            runStatus = false;                        // 进入急停状态，机床停止运动
            maxiGroupRotateSpeed = 0;                 // 主轴停止旋转
        }
    });
    $("#cycle_on").click(function () {
        if ($("#cycle_on").attr("src") == 'mintor/model/png/green_off.png') {
            $("#cycle_on").attr("src", 'mintor/model/png/green_on.png');
            $("#cycle_off").attr("src", 'mintor/model/png/red_off.png');
        }
    });
    $("#cycle_off").click(function () {
        if ($("#cycle_off").attr("src") == 'mintor/model/png/red_off.png') {
            $("#cycle_off").attr("src", 'mintor/model/png/red_on.png');
            $("#cycle_on").attr("src", 'mintor/model/png/green_off.png');
        }
        else $("#cycle_off").attr("src", 'mintor/model/png/red_off.png');
    });
    $("#press_singlerun_off").click(function () {
        if ($("#press_singlerun_off").attr("src") == 'mintor/model/png/press_singlerun_off.png') {
            $("#press_singlerun_off").attr("src", 'mintor/model/png/press_singlerun_on.png');
        }
        else $("#press_singlerun_off").attr("src", 'mintor/model/png/press_singlerun_off.png');
    });
    $("#press_norun_off").click(function () {
        if ($("#press_norun_off").attr("src") == 'mintor/model/png/press_norun_off.png') {
            $("#press_norun_off").attr("src", 'mintor/model/png/press_norun_on.png');
        }
        else $("#press_norun_off").attr("src", 'mintor/model/png/press_norun_off.png');
    });
    $("#press_selectjump_off").click(function () {
        if ($("#press_selectjump_off").attr("src") == 'mintor/model/png/press_selectjump_off.png') {
            $("#press_selectjump_off").attr("src", 'mintor/model/png/press_selectjump_on.png');
        }
        else $("#press_selectjump_off").attr("src", 'mintor/model/png/press_selectjump_off.png');
    });
    $("#press_selectstop_off").click(function () {
        if ($("#press_selectstop_off").attr("src") == 'mintor/model/png/press_selectstop_off.png') {
            $("#press_selectstop_off").attr("src", 'mintor/model/png/press_selectstop_on.png');
        }
        else $("#press_selectstop_off").attr("src", 'mintor/model/png/press_selectstop_off.png');
    });
    $("#press_machinelock_off").click(function () {
        if ($("#press_machinelock_off").attr("src") == 'mintor/model/png/press_machinelock_off.png') {
            $("#press_machinelock_off").attr("src", 'mintor/model/png/press_machinelock_on.png');
        }
        else $("#press_machinelock_off").attr("src", 'mintor/model/png/press_machinelock_off.png');
    });
    $("#press_zlock_off").click(function () {
        if ($("#press_zlock_off").attr("src") == 'mintor/model/png/press_zlock_off.png') {
            $("#press_zlock_off").attr("src", 'mintor/model/png/press_zlock_on.png');
        }
        else $("#press_zlock_off").attr("src", 'mintor/model/png/press_zlock_off.png');
    });
    $("#press_mstlock_off").click(function () {
        if ($("#press_mstlock_off").attr("src") == 'mintor/model/png/press_mstlock_off.png') {
            $("#press_mstlock_off").attr("src", 'mintor/model/png/press_mstlock_on.png');
        }
        else $("#press_mstlock_off").attr("src", 'mintor/model/png/press_mstlock_off.png');
    });
    $("#press_insert_off").click(function () {
        if ($("#press_insert_off").attr("src") == 'mintor/model/png/press_insert_off.png') {
            $("#press_insert_off").attr("src", 'mintor/model/png/press_insert_on.png');
        }
        else $("#press_insert_off").attr("src", 'mintor/model/png/press_insert_off.png');
    });

    $("#press_spindle_plus_off").click(function () {
        $("#press_spindle_plus_off").attr("src", 'mintor/model/png/press_spindle_plus_on.png');
        $("#press_spindle_stop_off").attr("src", 'mintor/model/png/press_spindle_stop_off.png');
        $("#press_spindle_minus_off").attr("src", 'mintor/model/png/press_spindle_minus_off.png');
        if (maxiGroupRotateSpeed < 0) maxiGroupRotateSpeed = maxiGroupRotateSpeed * -1.0;
        else if (maxiGroupRotateSpeed == 0) maxiGroupRotateSpeed = 0.1;
    });

    $("#press_spindle_stop_off").click(function () {
        $("#press_spindle_plus_off").attr("src", 'mintor/model/png/press_spindle_plus_off.png');
        $("#press_spindle_stop_off").attr("src", 'mintor/model/png/press_spindle_stop_on.png');
        $("#press_spindle_minus_off").attr("src", 'mintor/model/png/press_spindle_minus_off.png');
        maxiGroupRotateSpeed = 0;
    });

    $("#press_spindle_minus_off").click(function () {
        $("#press_spindle_plus_off").attr("src", 'mintor/model/png/press_spindle_plus_off.png');
        $("#press_spindle_stop_off").attr("src", 'mintor/model/png/press_spindle_stop_off.png');
        $("#press_spindle_minus_off").attr("src", 'mintor/model/png/press_spindle_minus_on.png');
        if (maxiGroupRotateSpeed > 0) maxiGroupRotateSpeed = maxiGroupRotateSpeed * -1.0;
        else if (maxiGroupRotateSpeed == 0) maxiGroupRotateSpeed = -0.1;
    });
    $("#press_reset").click(function () {
        ResetMachineMotion();
    });

    var timeout;
    $("#press_axis_plus_off").mousedown(function () {
        $("#press_axis_plus_off").attr("src", 'mintor/model/png/press_axis_plus_on.png');
        stepForwardStep(1);
    });
    $("#press_axis_plus_off").mouseup(function () {
        clearInterval(timeout);
        $("#press_axis_plus_off").attr("src", 'mintor/model/png/press_axis_plus_off.png');
    });
    $("#press_axis_minus_off").mousedown(function () {
        $("#press_axis_minus_off").attr("src", 'mintor/model/png/press_axis_minus_on.png');
        stepForwardStep(0);
    });
    $("#press_axis_minus_off").mouseup(function () {
        clearInterval(timeout);
        $("#press_axis_minus_off").attr("src", 'mintor/model/png/press_axis_minus_off.png');
    });
    function stepForwardStep(m_direction) {                         // 0表示负方向，1表示正方向
        clearInterval(timeout);
        var direction_plus = 1.0;
        if (m_direction == 0)  direction_plus = -1.0;
        else                direction_plus = 1.0;
        timeout = setInterval(function ()//setInterval会每100毫秒执行一次
        {
            switch (axis_sel_v) {
                case 0:
                    switch (rate_sel_v) {
                        case 0:
                            moveGrouptoRealPositionStep(machineMotion[0], 0, direction_plus * 0.001);
                            break;
                        case 1:
                            moveGrouptoRealPositionStep(machineMotion[0], 0, direction_plus * 0.01);
                            break;
                        case 2:
                            moveGrouptoRealPositionStep(machineMotion[0], 0, direction_plus * 0.1);
                            break;
                        default:
                            break;
                    }
                    break;
                case 1:
                    switch (rate_sel_v) {
                        case 0:
                            moveGrouptoRealPositionStep(machineMotion[1], 1, direction_plus * 0.001);
                            break;
                        case 1:
                            moveGrouptoRealPositionStep(machineMotion[1], 1, direction_plus * 0.01);
                            break;
                        case 2:
                            moveGrouptoRealPositionStep(machineMotion[1], 1, direction_plus * 0.1);
                            break;
                        default:
                            break;
                    }
                    break;
                case 2:
                    switch (rate_sel_v) {
                        case 0:
                            moveGrouptoRealPositionStep(machineMotion[2], 2, direction_plus * 0.001);
                            break;
                        case 1:
                            moveGrouptoRealPositionStep(machineMotion[2], 2, direction_plus * 0.01);
                            break;
                        case 2:
                            moveGrouptoRealPositionStep(machineMotion[2], 2, direction_plus * 0.1);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }, 100);
    }

    var axis_info_sel = 1;
    $("#left_t").click(function () {
        axis_info_sel--;
        if (axis_info_sel == 0) axis_info_sel = 3;
        changeAxisInfoDisplay();
    });
    $("#right_t").click(function () {
        axis_info_sel++;
        if (axis_info_sel == 4) axis_info_sel = 1;
        changeAxisInfoDisplay();
    });

    function changeAxisInfoDisplay() {
        switch (axis_info_sel) {
            case 1:
            {
                $("#pwr").hide();
                $("#fuhe").hide();
                $("#pos").show();
                break;
            }
            case 2:
            {
                $("#pos").hide();
                $("#fuhe").hide();
                $("#pwr").show();
                break;
            }
            case 3:
            {
                $("#pos").hide();
                $("#pwr").hide();
                $("#fuhe").show();
                break;
            }
            default:
                break;
        }
    }


    function init() {
//        var stats = initStats();                                // 初始化帧数显示插件
        container = document.createElement('div');

        //把上面的层放到body里
        document.body.appendChild(container);
        // create a scene, that will hold all our elements such as objects, cameras and lights.
        scene = new THREE.Scene();
        webGLRenderer = new THREE.WebGLRenderer({antialias: true});      // 生成渲染对象并设置抗锯齿为有效
        webGLRenderer.setPixelRatio(window.devicePixelRatio);
        webGLRenderer.setClearColor(0xaaaaff, 1.0);                            // 设置canvas背景色(clearColor)
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);     // 指定渲染的宽高
        webGLRenderer.shadowMapEnabled = true;
        camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 1e10);
        camera.position.set(-8, 3, -0.2);
        scene.add(camera);
        controls = new THREE.OrbitControls(camera);                             // 利用当前摄像机建立鼠标控制
        var dirLight = new THREE.DirectionalLight(0xffffff);  //方向光
        dirLight.position.set(130, 150, 1000).normalize();
        camera.add(dirLight);
        camera.add(dirLight.target);

        var objLoader = new THREE.VRMLLoader();
        objLoader.load('mintor/model/model.wrl', function (geometry) {
            totalMd = geometry;
            sensor = geometry.children[0];

            xaxismotion = new Array(geometry.children[0].children[3]);                  // X轴运动块数组
            yaxismotion = new Array(geometry.children[0].children[3].children[5]);      // Y轴运动块数组
            zaxismotion = new Array(                                                    // Z轴运动块数组
                    geometry.children[0].children[3].children[5].children[2],
                    geometry.children[0].children[3].children[5].children[3],
                    geometry.children[0].children[3].children[5].children[4],
                    geometry.children[0].children[3].children[5].children[5],
                    geometry.children[0].children[3].children[5].children[6]
            );
            machineMotion = new Array(xaxismotion, yaxismotion, zaxismotion);             // 机床运动块数组

            maxis = geometry.children[0].children[3].children[5].children[6].children[7];
            geometry.position.set(0, 0, 0);
            scene.add(geometry);
            container.appendChild(webGLRenderer.domElement);

            maxisGroup = new THREE.Group();                                      // 新建物体组
            var box1 = new THREE.Box3().setFromObject(maxis);               // 边界框
            maxisGroup.add(maxis);                                                 // 将目标块加入组中
            box1.getCenter(maxis.position);                                    // 设置盒子中心
            maxis.position.multiplyScalar(-1);                                // 反方向移动
            box1.getCenter(maxisGroup.position);
            scene.add(maxisGroup);

            maxisOrigin.x = maxisGroup.position.x;                            // 获取主轴原始坐标
            maxisOrigin.y = maxisGroup.position.y;
            maxisOrigin.z = maxisGroup.position.z;
            maxisGroup.position.set(maxisBase.x, maxisBase.y, maxisBase.z);   // 把主轴放到基准坐标
            maxisDeviation.x = maxisBase.x - maxisOrigin.x;                     // 计算主轴偏移
            maxisDeviation.y = maxisBase.y - maxisOrigin.y;
            maxisDeviation.z = maxisBase.z - maxisOrigin.z;
            axisBase.x = maxisDeviation.x - machineMotion[0][0].position.x;     // 计算三轴初始化后基准坐标
            axisBase.y = maxisDeviation.y - machineMotion[2][0].position.y;
            axisBase.z = maxisDeviation.z - machineMotion[1][0].position.z;
            initAllAxisGroup();                                                 // 把三轴的块移到基准零点
            container.addEventListener('mousemove', onMouseDownzaxis, false);
            container.addEventListener('mousedown', onMouseDownR, false);
        });
    }
    function initStats() {
        var stats = new Stats();
        stats.setMode(0); // 0: fps, 1: ms
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.left = '0px';
        stats.domElement.style.top = '0px';
        $("#Stats-output").append(stats.domElement);
        return stats;
    }
    function onMouseDownzaxis(event) {
        raycaster = new THREE.Raycaster();
        mouse = new THREE.Vector2();
        event.preventDefault();                                                    // 禁用事件的默认处理程序
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;                       // 计算鼠标坐标
        mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
        raycaster.setFromCamera(mouse, camera);                                   // 设置射线

        var intersects = raycaster.intersectObjects(sensor.children, true);
        var intersected;
        if (intersects.length > 0) {                                              // 是否拾取到物体
            intersected = intersects[0].object;
            if (intersected) {
                var throughBoxArray = intersects[0].object.matrixWorld.elements.toString();
                for (var i = 1; i < 91; i++) {
                    var zaxissensor = sensor.children[3].children[5].children[6].children[6].children[i];
                    if (throughBoxArray == zaxissensor.children[0].matrixWorld.elements.toString()) {
                        container.style.cursor = 'pointer';
                        zaxissensor.scale.set(3, 1.5, 2);
                        zaxissensor.children[0].children[0].material.color.set(0xff0000);

                        /*document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                         document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                        getData(event, temdatas, zaxissensor.name);

                        /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + zaxissensor.name + "</span><br/></p2>";
                         document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + temdata+ "度</span><br/></p2>";
                         $('#lbl_tip').show();*/
                        break;
                    } else {
                        if (i < 31 && throughBoxArray == sensor.children[0].children[i].children[0].matrixWorld.elements.toString()) {
                            var xleftsensor = sensor.children[0].children[i];
                            container.style.cursor = 'pointer';
                            xleftsensor.scale.set(3, 1.5, 2);
                            xleftsensor.children[0].children[0].material.color.set(0xff0000);
                            /* document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                             document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                            getData(event, temdatas, xleftsensor.name);
                            /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + xleftsensor.name + "</span><br/></p2>";
                             document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + i + "度</span><br/></p2>";
                             $('#lbl_tip').show();*/
                            break;
                        } else if (i < 31) {
                            sensor.children[0].children[i].scale.set(1, 1, 1);
                            sensor.children[0].children[i].children[0].children[0].material.color.set(0x00ffff);
                        }
                        if (i < 31 && throughBoxArray == sensor.children[1].children[i].children[0].matrixWorld.elements.toString()) {
                            var xrightsensor = sensor.children[1].children[i];
                            container.style.cursor = 'pointer';
                            xrightsensor.scale.set(3, 1.5, 2);
                            xrightsensor.children[0].children[0].material.color.set(0xff0000);
                            /*document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                             document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                            getData(event, temdatas, xrightsensor.name);
                            /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + xrightsensor.name + "</span><br/></p2>";
                             document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + i + "度</span><br/></p2>";
                             $('#lbl_tip').show();*/
                            break;
                        } else if (i < 31) {
                            sensor.children[1].children[i].scale.set(1, 1, 1);
                            sensor.children[1].children[i].children[0].children[0].material.color.set(0x00ffff);
                        }
                        if (i < 25 && throughBoxArray == sensor.children[3].children[4].children[i].children[0].matrixWorld.elements.toString()) {
                            var coleftsensor = sensor.children[3].children[4].children[i];
                            container.style.cursor = 'pointer';
                            coleftsensor.scale.set(3, 1.5, 2);
                            coleftsensor.children[0].children[0].material.color.set(0xff0000);
                            /*document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                             document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                            getData(event, temdatas, coleftsensor.name);
                            /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + coleftsensor.name + "</span><br/></p2>";
                             document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + i + "度</span><br/></p2>";
                             $('#lbl_tip').show();*/
                            break;
                        } else if (i < 25) {
                            sensor.children[3].children[4].children[i].scale.set(1, 1, 1);
                            sensor.children[3].children[4].children[i].children[0].children[0].material.color.set(0x00ffff);
                        }
                        if (i < 25 && throughBoxArray == sensor.children[3].children[3].children[i].children[0].matrixWorld.elements.toString()) {
                            var corightsensor = sensor.children[3].children[3].children[i];
                            container.style.cursor = 'pointer';
                            corightsensor.scale.set(3, 1.5, 2);
                            corightsensor.children[0].children[0].material.color.set(0xff0000);
                            /*document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                             document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                            getData(event, temdatas, corightsensor.name);
                            /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + corightsensor.name + "</span><br/></p2>";
                             document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + i + "度</span><br/></p2>";
                             $('#lbl_tip').show();*/
                            break;
                        } else if (i < 25) {
                            sensor.children[3].children[3].children[i].scale.set(1, 1, 1);
                            sensor.children[3].children[3].children[i].children[0].children[0].material.color.set(0x00ffff);
                        }
                        if (i < 21 && throughBoxArray == sensor.children[3].children[2].children[i].children[0].matrixWorld.elements.toString()) {
                            var yaxissensor = sensor.children[3].children[2].children[i];
                            container.style.cursor = 'pointer';
                            yaxissensor.scale.set(3, 1.5, 2);
                            yaxissensor.children[0].children[0].material.color.set(0xff0000);
                            /*document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
                             document.getElementById("lbl_tip").style.top = event.clientY + "px";*/
                            getData(event, temdatas, yaxissensor.name);
                            /*document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>001-SDT00-" + yaxissensor.name + "</span><br/></p2>";
                             document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + i + "度</span><br/></p2>";
                             $('#lbl_tip').show();*/
                            break;
                        } else if (i < 21) {
                            sensor.children[3].children[2].children[i].scale.set(1, 1, 1);
                            sensor.children[3].children[2].children[i].children[0].children[0].material.color.set(0x00ffff);
                        }
                        container.style.cursor = 'auto';
                        document.getElementById("lbl_tip").style.display = "none";
                        sensor.children[3].children[5].children[6].children[6].children[i].scale.set(1, 1, 1);
                        sensor.children[3].children[5].children[6].children[6].children[i].children[0].children[0].material.color.set(0x00ffff);
                    }
                }
            }
        }
        else {
            container.style.cursor = 'auto';
        }

        var intersectsm = raycaster.intersectObjects(maxis.children, true);
        var intersectedm;
        if (intersectsm.length > 0) {                                              // 是否拾取到物体
            intersectedm = intersectsm[0].object;
            if (intersectedm) {
                var throughBoxArraym = intersectsm[0].object.matrixWorld.elements.toString();
                for (var i = 1; i < 13; i++) {
                    if (throughBoxArraym == maxis.children[i].children[0].matrixWorld.elements.toString()) {
                        var maxissensor = maxis.children[i];
                        container.style.cursor = 'pointer';
                        maxissensor.scale.set(3, 1.5, 2);
                        maxissensor.children[0].children[0].material.color.set(0xff0000);
                        getData(event, temdatas, maxissensor.name);
                        break;
                    } else {
                        maxis.children[i].scale.set(1, 1, 1);
                        maxis.children[i].children[0].children[0].material.color.set(0x00ffff);
                    }
                }
            }
        }
    }


    var content=null;     //记录文本框中内容
    var commandindex = [];   //每个G指令中包含的参数
    var commandcode, Xdisplayment, Ydisplayment, Zdisplayment, speed, index = [], indexsort = [], codemap = [];   //命令行、G01指令、X位移、Y位移、Z位移、速度、XYZF在字符串中的位置集合、XYZF位置集合排序去除未出现的点、以键值对形式存储位移与方向
    var t0m,t0, t1, t2;


    $('#pickerbtn').click(function () {
        $('#picker').click();
    });
    var number = -1, gcodesplit, gcodearray,gcode,command;     //number用来记录执行G指令的次数

    /* 读取上传的文本文件内容 */
    function onFileSelected(event) {
        var selectedFile = event.target.files[0];
        var reader = new FileReader();
        var result = document.getElementById("result");
        reader.onload = function (event) {
            result.innerHTML = event.target.result;
            $('#result').show();
            $('#start').show();
            $('#stop').show();
//            content=event.target.result;

            content=result.innerHTML;
            /* 定义删除数组中元素的方法，相应下标发生改变 */
            Array.prototype.del = function (n) {　//n表示第几项，从0开始算起。
                //prototype为对象原型，注意这里为对象增加自定义方法的方法。
                if (n < 0)　//如果n<0，则不进行任何操作。
                    return this;
                else
                    return this.slice(0, n).concat(this.slice(n + 1, this.length));
            }
            gcodesplit = content.toString().split("G");
            gcodearray = gcodesplit.del(0);　//从0算起，删除第1项
//            readGcode();
        };
        reader.readAsText(selectedFile);
//        readGcode();
    }

    var filed=document.getElementById('pickerbtn'); //当机床在运动状态中，选着G代码文件不能点击
    var time,starttime, endtime,dingshi,tongjitime=[],clock1;
    /* 排序代码 */
    function sortNumber(a, b) {
        return a - b
    }

    /* 读取G代码中的字符G，获取指令集合 */
    function readGcode() {
        t0m=false;
        t0=false;
        t1=false;
        t2=[];
        number = number + 1;
        if(number<gcodearray.length){
            gcode = gcodearray[number];

            /* G代码没有格式要求，依据字符串切分 */
            if (gcode.indexOf(";") >= 0) {
                command = gcode.toString().split(";")[0];
            } else {
                command = gcode;
            }
            commandcode = command.substring(0, 2);
            index[0] = command.indexOf("X");
            if (index[0] < 0) {
                codemap["Xdisplayment"] = 0;
            }
            index[1] = command.indexOf("Y");
            if (index[1] < 0) {
                codemap["Ydisplayment"] = 0;
            }
            index[2] = command.indexOf("Z");
            if (index[2] < 0) {
                codemap["Zdisplayment"] = 0;
            }
            index[3] = command.indexOf("F");
            if (index[3] < 0) {
                codemap["Fdisplayment"] = speed;
            }
            index.sort(sortNumber);
            var delet = -1;
            for (var i = 0; i < 4; i++) {
                if (index[i] < 0) {
                    delet = i;
                }
            }
            if (delet >= 3) {
                alert("请输入正确的G代码");
            } else if (delet == 2) {
                indexsort = index.del(2).del(1).del(0);
            } else if (delet == 1) {
                indexsort = index.del(1).del(0);
            } else if (delet == 0) {
                indexsort = index.del(0);
            } else {
                indexsort = index;
            }

            for (i = 0; i < indexsort.length; i++) {
                var name = command.substring(indexsort[i], indexsort[i] + 1) + "displayment";
                codemap[name] = command.substring(indexsort[i] + 1, indexsort[i + 1]);
            }
            var xdis=parseFloat(codemap["Xdisplayment"]);
            var ydis=parseFloat(codemap["Ydisplayment"]);
            var zdis=parseFloat(codemap["Zdisplayment"]);
            Xdisplayment= xdis/5000*3;    //x轴行程5000，虚拟位置[-1,2],前为负，与实际相同
            Ydisplayment = ydis/650*0.6;      //x轴行程650，虚拟位置[-.3,0.3],下为负，与实际相同
            Zdisplayment = zdis*(-1)/4000;  //y轴行程4000，虚拟位置[-1,1],左为负，与实际相反
            speed = parseFloat(codemap["Fdisplayment"]);
            var pingfanghe = Math.pow(xdis, 2) + Math.pow(ydis, 2) + Math.pow(zdis, 2);
            total = Math.sqrt(pingfanghe);
            time = total / speed * 60000;   //speed单位是毫米每分钟，转换为毫秒
            if (time > 0) {
                tongjitime[number]=time;
                if(commandcode=="01"){
                    movebygcode();
                }
            }
        }
        else{
            alert("G代码运行完毕！");
            number = -1;
            gcodesplit=[];
            gcodearray=[];
            gcode=0;
            command=0;
            document.getElementById("result").innerHTML="";
            filed.disabled=false;
            var u1 =document.getElementById("result");
            u1.style.display="none";
            var u2 =document.getElementById("start");
            u2.style.display="none";
            u2.disabled=false;
            var u3 =document.getElementById("stop");
            u3.style.display="none";
            u3.disabled=true;
        }
    }

    /* 读取G代码，按照指定的轨迹运动 （01指令）*/
    function movebygcode() {     //  将某个轴移动到pos位置

        /* 主轴底部移动（包含xyz方向）*/
        var masis=maxisGroup;
        var maxisposition=maxisGroup.children[0];
        var pos00m = maxisGroup.position;
        var x0m = pos00m.x, y0m = pos00m.y, z0m = pos00m.z;
        x0m = Xdisplayment + x0m; y0m = Ydisplayment + y0m; z0m = Zdisplayment + z0m;
        var pos01m = {x: x0m, y: y0m, z: z0m};
        if (!t0m) {
            t0m = new TWEEN.Tween(pos00m)
                    .to(pos01m, time)
                    .onUpdate(function () {
                        maxisGroup.position = pos00m;
                    });
        }

        /* x轴方向运动*/
        var pos00 = machineMotion[0][0].position;
        var x0 = pos00.x, y0 = pos00.y, z0 = pos00.z;
        x0 = Xdisplayment + x0;
        var pos01 = {x: x0, y: y0, z: z0};
        if (!t0) {
            t0 = new TWEEN.Tween(pos00)
                    .to(pos01, time)
                    .onUpdate(function () {
                        pos00 = pos00;
                    });
        }

        /* y轴方向运动*/
        var pos10 = machineMotion[1][0].position;
        var x1 = pos10.x, y1 = pos10.y, z1 = pos10.z;
        z1 = Zdisplayment + z1;
        var pos11 = {x: x1, y: y1, z: z1};
        if (!t1) {
            t1 = new TWEEN.Tween(pos10)
                    .to(pos11, time)
                    .onUpdate(function () {
                        pos10 = pos10;
                    });
        }

        /* z轴方向运动*/
        for (var iTem=0;iTem<5;iTem++ ) {
            var pos20 = machineMotion[2][iTem].position;
            var x2 = pos20.x, y2 = pos20.y, z2 = pos20.z;
            y2 = Ydisplayment + y2;
            var pos21 = {x: x2, y: y2, z: z2};
            if (!t2[iTem]) {
                t2[iTem] = new TWEEN.Tween(pos20)
                        .to(pos21, time)
                        .onUpdate(function () {
                            pos20 = pos20;
                        });
            }
        }
        /* 动画启动*/
        t0m.start();
        t0.start();
        t1.start();
        t2[0].start();
        t2[1].start();
        t2[2].start();
        t2[3].start();
        t2[4].start();

        starttime = new Date().getTime();

//        dingshi=time;
        clock1=window.setTimeout("readGcode()",time);    //定时器
    }

    var stop = document.getElementById('stop');

    //    var judge=true;     //false表示当前按钮为停止按钮，true表示为开始按钮
    stop.onclick = function () {
//        judge=!judge;
//        if(!judge){
//            $('#stop').text("start");
        t0m.stop();
        t0.stop();
        t1.stop();
        t2[0].stop();
        t2[1].stop();
        t2[2].stop();
        t2[3].stop();
        t2[4].stop();
        endtime = new Date().getTime();
//        }else{
//            $('#stop').text("stop");
//            t0m.start();
//            t0.start();
//            t1.start();
//            t2[0].start();
//            t2[1].start();
//            t2[2].start();
//            t2[3].start();
//            t2[4].start();
//        }
        filed.disabled=false;
        dingshi=endtime-starttime;
        for (var itime in tongjitime) {
            dingshi=dingshi-tongjitime[itime];
        }
        window.clearTimeout(clock1);
        number=number-1;
        document.getElementById('stop').disabled=true;
        document.getElementById('start').disabled=false;

//        alert(dingshi);
    };

    var start = document.getElementById('start');
    start.onclick = function () {
        filed.disabled=true;
        document.getElementById('start').disabled=true;
        document.getElementById('stop').disabled=false;
        readGcode();
    };


    var poswebSocket = new WebSocket('ws://' + socketPath + '/websocket/001-POS');

    poswebSocket.onerror = function (event) {			// socket错误响应事件
        onError(event);
    };
    poswebSocket.onopen = function (event) {			// socket开启响应事件
        onOpen(event);
    };
    poswebSocket.onmessage = function (event) {		// socket接收响应事件
        var data = JSON.parse(event.data);
        if (data.value != "0.0" && data.value != "0,0,0,0" && data.value != "") {
            var temvalue = data.value.toString();
            postionarr = temvalue.split(",");
            xaxis = parseFloat(postionarr[0]);
            yaxis = parseFloat(postionarr[1]);
            zaxis = parseFloat(postionarr[2]);
            mspeed = parseFloat(postionarr[3]);
            moveGrouptoRealPosition(machineMotion[0], 0, (-2500 - xaxis) * 3 / 2500);
            moveGrouptoRealPosition(machineMotion[1], 1, (2000 + yaxis) * 3 / 2000);
            moveGrouptoRealPosition(machineMotion[2], 2, zaxis * 1.3 / 650);
            document.getElementById("xpos").innerHTML = "<p2 id='xpos'>x轴坐标 ：<span class='c'>" + xaxis.toFixed(4) + "</span><br/></p2>";
            document.getElementById("ypos").innerHTML = "<p2 id='ypos'>y轴坐标 ：<span class='c'>" + yaxis.toFixed(4) + "</span><br/></p2>";
            document.getElementById("zpos").innerHTML = "<p2 id='zpos'>z轴坐标 ：<span class='c'>" + zaxis.toFixed(4) + "</span><br/></p2>";
            document.getElementById("maspd").innerHTML = "<p2 id='maspd'>主轴速度：<span class='c'>" + mspeed.toFixed(4) + "</span><br/></p2>";
            onMessage(event);
        }
    };


    function onOpen(event) {
    }

    function onError(event) {
        alert(event.data);
    }

    function onMessage(event) {
        console.log(event.data);
    }

    function getData(event, temdatas, name) {
        var start = temdatas.indexOf(name);   //根据传感器名字查找其在字符串中开始位置
        if (start > 0) {
            var end = temdatas.indexOf(",", start + 15);         //查找数据结束位置
            temdata = temdatas.substring(start + 15, end - 1);
        } else {
            temdata = "0";
        }
        document.getElementById("lbl_tip").style.left = event.clientX + 10 + "px";
        document.getElementById("lbl_tip").style.top = event.clientY + "px";
        document.getElementById("sensorNum").innerHTML = "<p2 id='sensorNum'>传感器编号: <span class='c'>" + name + "</span><br/></p2>";
        if (temdata.indexOf("/") > 0) {
            document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值（FBG/DBS1820）: <span class='c'> " + temdata + "度</span><br/></p2>";
        } else {
            document.getElementById("sensorValue").innerHTML = "<p2 id='sensorValue'>数值: <span class='c'> " + temdata + "度</span><br/></p2>";
        }
        $('#lbl_tip').show();
    }

    function onMouseDownR(event) {
        if (event.button == 1) {                        // 滚轮键
            xdir = !xdir;
        }
        if (event.button == 0) {                        // 左键
            ydir = !ydir;
        }
        if (event.button == 2) {                        // 右键
            zdir = !zdir;
        }
    }
    ;
    $('#example_show').click(function (event) {
        runStatus = !runStatus;
        if (runStatus) {
            maxiGroupRotateSpeed = 0.1;
            $('#example_show').text("停止");
        }
        else {
            maxiGroupRotateSpeed = 0;
            $('#example_show').text("演示");
        }
    });
    function moveGroupPositionX(tempGroup) {
        if (getRealMaxisGroupPosition(0) < -3.0 && xdir == false || getRealMaxisGroupPosition(0) > 3.0 && xdir == true) return;
        for (var iTem in tempGroup) {
            tempGroup[iTem].position.x -= 0.001 * (xdir == false ? -1 : 1);      // 表示x轴运动
        }
        maxisGroup.position.x -= 0.001 * (xdir == false ? -1 : 1);              // 主轴x方向跟随x轴运动
    }
    function moveGroupPositionY(tempGroup) {
        if ((getRealMaxisGroupPosition(1) < -3.0 && ydir == false)           // 到达限位之后停止运动
                || (getRealMaxisGroupPosition(1) > 3.0 && ydir == true)) return;
        for (var iTem in tempGroup) {
            tempGroup[iTem].position.z += 0.001 * (ydir == true ? -1 : 1);       // 表示y轴运动
        }
        maxisGroup.position.z += 0.001 * (ydir == true ? -1 : 1);                // 主轴y方向跟随y轴运动
    }
    function moveGroupPositionZ(tempGroup) {
        if ((getRealMaxisGroupPosition(2) < -0.3 && zdir == false)           // 到达限位之后停止运动
                || (getRealMaxisGroupPosition(2) > 1.0 && zdir == true)) return;
        for (var iTem in tempGroup) {
            tempGroup[iTem].position.y += 0.001 * (zdir == false ? -1 : 1);        // 表示z轴运动
        }
        maxisGroup.position.y += 0.001 * (zdir == false ? -1 : 1);                 // 主轴z方向跟随z轴运动
    }

    function initAxisGrouptoPosition(tempGroup, taxis, pos) {
        for (var iTem in tempGroup) {
            switch (taxis) {
                case 0:
                    tempGroup[iTem].position.x = pos;
                    break;           // x轴移动到目的位置
                case 1:
                    tempGroup[iTem].position.z = pos;
                    break;           // y轴移动到目的位置
                case 2:
                    tempGroup[iTem].position.y = pos;
                    break;           // z轴移动到目的位置
                default:
                    break;
            }
        }
    }
    function initAllAxisGroup() {                                       // 机床三轴根据主轴偏移初始化函数
        initAxisGrouptoPosition(machineMotion[0], 0, axisBase.x);
        initAxisGrouptoPosition(machineMotion[1], 1, axisBase.z);
        initAxisGrouptoPosition(machineMotion[2], 2, axisBase.y);
    }
    function ResetMachineMotion() {
        moveGrouptoRealPosition(machineMotion[0], 0, 0);
        moveGrouptoRealPosition(machineMotion[1], 1, 0);
        moveGrouptoRealPosition(machineMotion[2], 2, 0);
    }

    function infoShowChange() {
        var xpos = getRealMaxisGroupPosition(0);
        var ypos = getRealMaxisGroupPosition(1);
        var zpos = getRealMaxisGroupPosition(2);
        var maspd = getRealMaxisGroupPosition(3);

//        document.getElementById("maspd").innerHTML = "<p2 id='maspd'>主轴速度：<span class='c'>" + maspd.toFixed(3) + "</span><br/></p2>";

        if (axis_info_sel != 1) return;                                    // 不是坐标显示时不显示
//        document.getElementById("xpos").innerHTML = "<p2 id='xpos'>x轴坐标 ：<span class='c'>" + xpos.toFixed(3) + "</span><br/></p2>";
//        document.getElementById("ypos").innerHTML = "<p2 id='ypos'>y轴坐标 ：<span class='c'>" + ypos.toFixed(3) + "</span><br/></p2>";
//        document.getElementById("zpos").innerHTML = "<p2 id='zpos'>z轴坐标 ：<span class='c'>" + zpos.toFixed(3) + "</span><br/></p2>";
    }
    function getRealMaxisGroupPosition(axis_which) {         //  参数为0表示x方向，1表示y方向
        //  2表示z方向，3表示转速
        if (axis_which == 0) return (maxisGroup.position.x - maxisBase.x) * -1.0;
        if (axis_which == 1) return (maxisGroup.position.z - maxisBase.z) * -1.0;
        if (axis_which == 2) return (maxisGroup.position.y - maxisBase.y) * 1.0;
//        if(axis_which==3) return maxisGroup.rotation.y * -1.0;
        if (axis_which == 3) return maxiGroupRotateSpeed;
    }
    function rotatyMaxisGroupPosition(m_speed) {                //  参数为旋转速度
        maxisGroup.rotateY(m_speed * -1.0);                       // 主轴旋转动作
    }
    function moveGrouptoRealPosition(tempGroup, taxis, pos) {     //  讲某个轴移动到pos位置
        for (var iTem in tempGroup) {
            switch (taxis) {
                case 0:
                    tempGroup[iTem].position.x = (pos / -1.0 + maxisBase.x - maxisGroup.position.x)
                            + tempGroup[iTem].position.x;
                    break;           // x轴移动到目的位置
                case 1:
                    tempGroup[iTem].position.z = (pos / -1.0 + maxisBase.z - maxisGroup.position.z)
                            + tempGroup[iTem].position.z;
                    break;           // y轴移动到目的位置
                case 2:
                    tempGroup[iTem].position.y = (pos / 1.0 + maxisBase.y - maxisGroup.position.y)
                            + tempGroup[iTem].position.y;
                    break;           // z轴移动到目的位置
                default:
                    break;
            }
        }
        switch (taxis) {
            case 0:
                maxisGroup.position.x = pos / -1.0 + maxisBase.x;
                break; // 主轴x方向移动到目的位置
            case 1:
                maxisGroup.position.z = pos / -1.0 + maxisBase.z;
                break; // 主轴y方向移动到目的位置
            case 2:
                maxisGroup.position.y = pos / 1.0 + maxisBase.y;
                break;  // 主轴z方向移动到目的位置
            default:
                break;
        }
    }
    function moveGrouptoRealPositionStep(tempGroup, taxis, m_step) {          //  讲某个轴移动m_step个单位
        for (var iTem in tempGroup) {
            switch (taxis) {
                case 0:
                    tempGroup[iTem].position.x = m_step * -1.0 + tempGroup[iTem].position.x;
                    break;           // x轴移动到目的位置
                case 1:
                    tempGroup[iTem].position.z = m_step * -1.0 + tempGroup[iTem].position.z;
                    break;           // y轴移动到目的位置
                case 2:
                    tempGroup[iTem].position.y = m_step * 1.0 + tempGroup[iTem].position.y;
                    break;           // z轴移动到目的位置
                default:
                    break;
            }
        }
        switch (taxis) {
            case 0:
                maxisGroup.position.x = (getRealMaxisGroupPosition(taxis) + m_step) / -1.0 + maxisBase.x;
                break; // 主轴x方向移动到目的位置
            case 1:
                maxisGroup.position.z = (getRealMaxisGroupPosition(taxis) + m_step) / -1.0 + maxisBase.z;
                break; // 主轴y方向移动到目的位置
            case 2:
                maxisGroup.position.y = (getRealMaxisGroupPosition(taxis) + m_step) / 1.0 + maxisBase.y;
                break;  // 主轴z方向移动到目的位置
            default:
                break;
        }
    }

    //    function moveGrouptoRealPositionAtSpeed(tempGroup,taxis,pos,m_speed) {     //  讲某个轴移动到pos位置
    //        if(m_speed<0) return;
    //        var displacement;
    //        var n,lef;
    //        for(var iTem in tempGroup){
    //            switch(taxis){
    //                case 0:
    //                    displacement = (pos/-1.0 + maxisBase.x-maxisGroup.position.x);
    //
    //                    lef=displacement % m_speed;                        // 余数
    //                    n=parseInt(displacement / m_speed);                // 循环次数
    //                    for(var i=0;i<n;i++){
    //                        tempGroup[iTem].position.x=m_speed
    //                            +tempGroup[iTem].position.x;                // x轴移动到目的位置
    //                    }                                                   // 有余数就移动余数部分
    //                    if(lef >= 0) tempGroup[iTem].position.x=lef + tempGroup[iTem].position.x;
    //                    break;
    //                case 1:
    //                    tempGroup[iTem].position.z=(pos/-1.0 + maxisBase.z-maxisGroup.position.z)
    //                        +tempGroup[iTem].position.z; break;           // y轴移动到目的位置
    //                case 2:
    //                    tempGroup[iTem].position.y=(pos/1.0 + maxisBase.y-maxisGroup.position.y)
    //                        +tempGroup[iTem].position.y; break;           // z轴移动到目的位置
    //                default:break;
    //            }
    //        }
    //        switch(taxis){
    //            case 0:maxisGroup.position.x = pos/-1.0 + maxisBase.x; break; // 主轴x方向移动到目的位置
    //            case 1:maxisGroup.position.z = pos/-1.0 + maxisBase.z; break; // 主轴y方向移动到目的位置
    //            case 2:maxisGroup.position.y = pos/ 1.0 + maxisBase.y; break;  // 主轴z方向移动到目的位置
    //            default:break;
    //        }
    //

    function render(times) {
        if (runStatus == true) {
            if (maxisGroup != undefined) {
                rotatyMaxisGroupPosition(maxiGroupRotateSpeed);
                maxisGroup.zoomSpeed = 1.2;
            }
            if (xaxismotion != undefined) {
                moveGroupPositionX(machineMotion[0]);                  // 移动x轴块
                moveGroupPositionY(machineMotion[1]);                  // 移动y轴块
                moveGroupPositionZ(machineMotion[2]);                  // 移动z轴块
            }
        }
        if (maxisGroup != undefined) infoShowChange();
        requestAnimationFrame(render);       //以合适的时间间隔执行render()方法，通常是60次每秒
        TWEEN.update(times);
        controls.update();
        webGLRenderer.render(scene, camera);
    }


</script>


</body>
</html>